<?php

namespace App\Http\Controllers\Admin;

use App\Admin\Admin;
use App\Admin\Message;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Input;

class MessageController extends Controller
{
    //get Admin/user 全部用户列表
    public function index(Request $request)
    {
        $mess = Message::select()->get();
        $count = count($mess);
        //2、设置每页显示条数
        $rev = '4';
        //3、求总页数
        $sums = ceil($count/$rev);
        //4、求当前页
        $page = Input::get('page');
//        return $page;
        if(empty($page)){
            $page = "1";
        }
        $page = $page>0?$page:1;
        $page = $page<$sums?$page:$sums;
        //5、设置上一页、下一页
        $prev = ($page-1)>0?$page-1:1;
        $next = ($page+1)<$sums?$page+1:$sums;
        //6、求偏移量
        $offset = ($page-1)*$rev;
        $mess = Message::select()
            ->where('content','like','%'.$request->data.'%')
            ->offset($offset)
            ->limit($rev)
            ->get();
        if($mess==[]){
            $page = ($page-1)>0?$page-1:1;
        }
        return view('admin.message.messlist',compact('mess','count','page','sums'));
    }
//
    public function chanf(Request $request)
    {

    }
    public function show(Request $request)
    {

        $mess = Message::select()->where('admin_name','like','%'.$request->data.'%')->get();
        //1、查询数据库总条数
        $count = count($mess);
        //2、设置每页显示条数
        $rev = '4';
        //3、求总页数
        $sums = ceil($count/$rev);
        //4、求当前页
        $page = Input::get('page');
//        return $page;
        if(empty($page)){
            $page = "1";
        }
        $page = $page>0?$page:1;
        $page = $page<$sums?$page:$sums;
        //5、设置上一页、下一页
        $prev = ($page-1)>0?$page-1:1;
        $next = ($page+1)<$sums?$page+1:$sums;
        //6、求偏移量
        $offset = ($page-1)*$rev;
        $mess = Message::select()
            ->where('admin_name','like','%'.$request->data.'%')
            ->offset($offset)
            ->limit($rev)
            ->get()->toArray();
//        return $request->data;
        if($request->isMethod('post')) {
            $data='';
            if(!empty($mess)){
                foreach($mess as $u) {
                    $data .=
                        "<tr><td style='text-align:center;line-height:50px'>"
                        . $u['id'] .
                        "</td><td style='text-align:center;line-height:50px'>"
                        . $u['admin_name'] ."</td>".
                        " <td style='text-align:center;line-height:50px'>".$u['user_name']."</td>".
                        " <td style='text-align:center;line-height:50px'>".$u['content']."</td>".
                        " <td style='text-align:center;line-height:50px'>" .date("Y-m-d H:m:s", $u['ctime']) . "</td>" .
                        "<td style='text-align:center;line-height:50px'>
                    <button type='button' class='btn btn-success'>".
                        "<a href=\"user/".$u['id']."/edit?page=".$page."\" style=\"color:white\">修改</a></button> ".
                        "<button type=\"button\" class='btn btn-danger'>
                    <a href=\"javascript:\" onclick=\"delArt(".$u['id'].")\" style=\"color:white\">删除</a>
                    </button>
                    ".
                        "</td>".
                        "</tr>";
                }
                $a = "<a href='javascript:void(0)' onclick='page(1)'>首页</a><a href='javascript:void(0)' onclick='page(" . $prev . ")'>上一页</a><a href='javascript:void(0)' onclick='page(" . $next . ")'>下一页</a><a href='javascript:void(0)' onclick='page(" . $sums . ")'>尾页</a>";
                $arr = [
                    'data' => $data,
                    'page' => $a,
                    'count' => $count,
                    'ever' => $page,
                    'all' => $sums,
                    'msg' => '1',
                ];
                $json = json_encode($arr, JSON_UNESCAPED_UNICODE);
                return $json;
            }else{
                $data= "<th scope='col' style='text-align: center' colspan='7'>暂无用户</th>";
                $arr=['data'=>$data,'msg'=>0];
                $json = json_encode($arr,JSON_UNESCAPED_UNICODE);
                return $json;
            }
        }
        $data='';
        if(!empty($mess)) {
            foreach($mess as $u) {
                $data .=
                    "<tr><td style='text-align:center;line-height:50px'>"
                    . $u['id'] .
                    "</td><td style='text-align:center;line-height:50px'>"
                    . $u['admin_name'] ."</td>".
                    " <td style='text-align:center;line-height:50px'>".$u['user_name']."</td>".
                    " <td style='text-align:center;line-height:50px'>".$u['content']."</td>".
                    " <td style='text-align:center;line-height:50px'>" .date("Y-m-d H:m:s", $u['ctime']) . "</td>" .
                    "<td style='text-align:center;line-height:50px'>
                    <button type='button' class='btn btn-success'>".
                    "<a href=\"user/".$u['id']."/edit?page=".$page."\" style=\"color:white\">修改</a></button> ".
                    "<button type=\"button\" class='btn btn-danger'>
                    <a href=\"javascript:\" onclick=\"delArt(".$u['id'].")\" style=\"color:white\">删除</a>
                    </button>
                    ".
                    "</td>".
                    "</tr>";

            }
            $a = "<a href='javascript:void(0)' onclick='page(1)'>首页</a><a href='javascript:void(0)' onclick='page(" . $prev . ")'>上一页</a><a href='javascript:void(0)' onclick='page(" . $next . ")'>下一页</a><a href='javascript:void(0)' onclick='page(" . $sums . ")'>尾页</a>";
            $arr = [
                'data' => $data,
                'page' => $a,
                'count' => $count,
                'ever' => $page,
                'all' => $sums,
                'msg' => '1',
            ];
            $json = json_encode($arr, JSON_UNESCAPED_UNICODE);
            return $json;

        }else{
            $data= "<th scope='col' style='text-align: center' colspan='7'>暂无用户</th>";
            $a = "<a href='javascript:void(0)' onclick='page(1)'>首页</a><a href='javascript:void(0)' onclick='page(1)'>上一页</a><a href='javascript:void(0)' onclick='page(1)'>下一页</a><a href='javascript:void(0)' onclick='page(1)'>尾页</a>";
            $arr=[
                'data'=>$data,
                'count'=>$count,
                'page'=>$a,

            ];
            $json = json_encode($arr,JSON_UNESCAPED_UNICODE);
            return $json;
        }

    }
    //post Admin/user 添加用户信息提交
    public function store(Request $request)
    {

    }
    //get Admin/user/{user}/edit   编辑用户信息
    public function edit($user_id)
    {
        $page = Input::get('page');
//        return $page;
        if(empty($page)){
            $page = "1";
        }
        $mess = Message::find($user_id);
        return view('admin/message/messedit',compact('mess','page'));

    }
    //put Admin/navs/{navs} 更新用户信息
    public function update($id)
    {
        $page = Input::get('page');
//        return $page;
        if(empty($page)){
            $page = "1";
        }
        $input = Input::except('_token','_method','page');
        $res = Message::where('id',$id)->update($input);
        if($res){
            return redirect('admin/message?page='.$page);
        }else{
            return back()->with('errors','用户更新失败');
        }

    }

    //delete Admin/nav/{nav}  删除用户信息
    public function destroy($id)
    {
        $res = Message::where('id',$id)->delete();
        if($res){
            $data = [
                'status'=>0,
                'msg'=>'留言删除成功!',
            ];
        }else{
            $data = [
                'status'=>1,
                'msg'=>'留言删除失败!',
            ];
        }
        return $data;
    }

    public function mess()
    {
        $admin = session('admin');
        $id = $admin[0]->id;
        return view('admin.message',compact('id'));
    }
    public function messadd(Request $request)
    {
        $data = Input::all();
        $input = Input::except('_token');
        $content = $input['value'];
        $id = $input['hid'];
        $user = Admin::where('id', $id)->select()->get();
        $admin_name = $user[0]->username;
        $user_name = $input['val'];
        $res = Admin::where('username', $user_name)->select()->get()->toArray();
        if (!$res) {
            $data = [
                'status' => 1,
                'msg' => '用户不存在!',
            ];
            return $data;
        }
        if($admin_name==$user_name){
            $data = [
                'status' => 1,
                'msg' => '不能自己给自己留言!',
            ];
            return $data;
        }
        $result=[];
        $result['content']=$content;
        $result['admin_name']=$admin_name;
        $result['user_name']=$user_name;
        $result['ctime']=time();
        $res = Message::create($result);
        if($res){
            $data = [
                'status'=>0,
                'msg'=>'留言成功!',
            ];
        }else{
            $data = [
                'status'=>1,
                'msg'=>'留言失败!',
            ];
        }
        return $data;
    }
    public function find()
    {
        $name=[];
        $admin = session('admin');
        $username= $admin[0]->username;
        $name = Message::where('user_name', $username)->paginate(1);
        return view('admin.messfind',compact('name'));
    }

}
